
//
//  JsonAnalysis.m
//  成品家
//
//  Created by YYY on 2017/3/7.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "JsonAnalysis.h"

@implementation JsonAnalysis

//字典转json格式字符串
+(NSString *)dictionaryAnalysisToJson:(NSDictionary *)dictionary
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

//数组转json格式字符串
+(NSString *)arrayAnalysisToJson:(NSArray *)array
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (id)jsonAnalysisToArrayOrDictWithJsonString:(NSString *)jsonString
{
    NSError *error;
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if (error)
    {
        return nil;
    }
    return obj;
}

@end
